package com.liang.maogouyuyin.ui.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.liang.maogouyuyin.bean.PetBean
import com.liang.maogouyuyin.local.LocalRepository
import kotlinx.coroutines.launch

class PetViewModel: ViewModel() {
    private val _petList = MutableLiveData<ArrayList<PetBean>>()
    val petList get() = _petList

    private val _pet = MutableLiveData<PetBean>()
    val pet get() = _pet

    fun savePet(profile: String, name: String, sex: Boolean, birthday: Long, arriveHome: Long) {
        val bean = PetBean(
            profile, name, sex, birthday, arriveHome
        )
        viewModelScope.launch {
            LocalRepository.savePet(bean)
        }
    }

    fun deletePet(bean: PetBean) {
        viewModelScope.launch {
            LocalRepository.deletePet(bean)
        }
    }

    fun updatePet(bean: PetBean) {
        viewModelScope.launch {
            LocalRepository.updatePet(bean)
        }
    }

    fun loadAll() {
        viewModelScope.launch {
            _petList.value = LocalRepository.loadPet()
        }
    }

    fun loadPetByName(name: String) {
        viewModelScope.launch {
            _pet.value = LocalRepository.loadPetByName(name)
        }
    }
}